package view;


import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import table.cellrenderer.BetragTableCellRenderer;
import table.tablemodel.KontoUebersichtTableModel;

import components.ComponentUtil;

public class KontoUebersichtDialog extends JDialog{

	private static final long serialVersionUID = 100L;

	private JPanel jContentPane;
	private JLabel kundennummerLabel;

	private JTextField tfKundennummer;
	private JTable kontenTable;

	private JButton btZeigeKonten;
	private JButton btEnde;

    private KontoUebersichtTableModel kontoUebersichtTableModel;


	public KontoUebersichtDialog(Frame owner, boolean b) {
		super(owner);
		initialize();
		setVisible(b);
	}

	private void initialize() {
		setSize(600, 500);
		setTitle("Bank-Anwendung - Kontostandsübersicht anzeigen");
		setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			kundennummerLabel = new JLabel("Kundennummer");
			kundennummerLabel.setBorder(new EmptyBorder(0, 4, 0, 4));

			JPanel topLeftPanel = new JPanel(new BorderLayout());
			topLeftPanel.add(kundennummerLabel, BorderLayout.WEST);
			topLeftPanel.add(getTfKundennummer(), BorderLayout.CENTER);

			JPanel topLeftWrapPanel = new JPanel(new BorderLayout());
			topLeftWrapPanel.add(topLeftPanel, BorderLayout.NORTH);

			JPanel topRightPanel = new JPanel(new GridLayout(2, 1));
			topRightPanel.add(getBtZeigeKonten());
			topRightPanel.add(getBtEnde());

			JPanel topPanel = new JPanel(new BorderLayout());
			topPanel.setBorder(new EmptyBorder(4, 2, 4, 2));
			topPanel.add(topLeftWrapPanel, BorderLayout.CENTER);
			topPanel.add(topRightPanel, BorderLayout.EAST);

			jContentPane = new JPanel(new BorderLayout());
			jContentPane.add(topPanel, BorderLayout.NORTH);
			jContentPane.add(new JScrollPane(getKontenTable()), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	private JTable getKontenTable() {
        if (kontenTable == null) {
            kontenTable = new JTable(getKontoUebersichtTableModel());
            kontenTable.setDefaultRenderer(Double.class, new BetragTableCellRenderer());
        }
        return kontenTable;
    }

	public KontoUebersichtTableModel getKontoUebersichtTableModel() {
	    if (kontoUebersichtTableModel == null) {
	        kontoUebersichtTableModel = new KontoUebersichtTableModel();
	    }
	    return kontoUebersichtTableModel;
	}

    public JTextField getTfKundennummer() {
		if (tfKundennummer == null) {
			tfKundennummer = new JTextField();
			ComponentUtil.addEnterKeyDelegator(tfKundennummer, getBtZeigeKonten());
		}
		return tfKundennummer;
	}

	public JButton getBtZeigeKonten() {
		if (btZeigeKonten == null) {
			btZeigeKonten = new JButton("Zeige Konten");
			ComponentUtil.addDefaultButtonBehaviour(btZeigeKonten);
		}
		return btZeigeKonten;
	}

	public JButton getBtEnde() {
		if (btEnde == null) {
			btEnde = new JButton("Beenden");
			ComponentUtil.addDefaultButtonBehaviour(btEnde);
		}
		return btEnde;
	}
}
